package diary

import (
	"context"
	"time"

	"go-weixin/app/diary/api/internal/svc"
	"go-weixin/app/diary/api/internal/types"
	"go-weixin/app/diary/rpc/pb/proto/diary"

	"github.com/zeromicro/go-zero/core/logx"
)

type DiaryQueryTitleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDiaryQueryTitleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DiaryQueryTitleLogic {
	return &DiaryQueryTitleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DiaryQueryTitleLogic) DiaryQueryTitle(req *types.DiaryQueryTitleReq) (resp *types.DiaryQueryTitleResp, err error) {
	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	res, err := l.svcCtx.DiaryRpc.DiaryQueryTitle(ctx, &diary.DiaryQueryTitleReq{
		Title: req.Title,
		Page:  req.Page,
	})
	if err != nil {
		return nil, err
	}
	var diaryList []types.Diary
	if len(res.DiaryList) > 0 {
		for _, d := range res.DiaryList {
			diaryList = append(diaryList, types.Diary{
				Id:       d.Id,
				UserId:   d.UserId,
				Title:    d.Title,
				Content:  d.Content,
				CreateAt: d.CreateAt,
				UpdateAt: d.UpdateAt,
				DeleteAt: d.DeleteAt,
			})
		}
	}
	resp = new(types.DiaryQueryTitleResp)
	resp.DiaryList = diaryList
	return
}
